package com.tensquare.qa.controller;

import com.tensquare.qa.pojo.Problem;
import com.tensquare.qa.service.QaService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("problem")
public class QaController {
    @Autowired
    private QaService qaService;

    /**
     * 1.查询最新的问答列表信息
     * @param id    问答id
     * @param page  当前页
     * @param size  页大小
     * @return  问答列表信息
     */
    @GetMapping("newlist/{id}/{page}/{size}")
    public Result newlist(@PathVariable String id,@PathVariable int page,@PathVariable int size){
        try {
            PageResult<Problem> problemPageResult = qaService.newlist(id,page,size);
            return new Result(StatusCode.OK,true,"查询最新的问答列表成功！",problemPageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR,false,"查询最新的问答列表失败！");
        }
    }

    /**
     * 2.查询热门的问答列表信息
     * @param id    问答id
     * @param page  当前页
     * @param size  页大小
     * @return  分页的问答列表信息
     */
    @GetMapping("hotlist/{id}/{page}/{size}")
    public Result hotlist(@PathVariable String id,@PathVariable int page,@PathVariable int size){
        try {
            PageResult<Problem> problemPageResult = qaService.hotlist(id,page,size);
            return new Result(StatusCode.OK,true,"查询热门的问答列表成功！",problemPageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR,false,"查询热门的问答列表失败！");
        }
    }
}
